package com.ingenio.siot.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.ingenio.siot.adapter.MovilAdapter;
import com.ingenio.siot.adapter.TipoMovilAdapter;
import com.ingenio.siot.entity.Movil;

/**
 * Servlet implementation class ABMTipoMovilServlet
 */
@WebServlet("/ABMMovil")
public class ABMMovilServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ABMMovilServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MovilAdapter ma = new MovilAdapter();
		Movil m = null;
		TipoMovilAdapter tma = new TipoMovilAdapter();
		JSONObject jo = new JSONObject();
		if("eliminar".equals(request.getParameter("type"))){
			try {
				jo.put("action", "eliminar");
				Integer id = Integer.parseInt(request.getParameter("id"));
		    	ma.eliminarMovil(id);
				jo.put("id", id);
		    	jo.put("error", false);
			} 
			catch (Exception e) {
				try {
					jo.put("error", true);
				} catch (JSONException e1) {
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		}else if("editar".equals(request.getParameter("type"))){
			if(true){
				Integer id = Integer.parseInt(request.getParameter("id"));
				try {
					jo.put("action", "editar");
					m = ma.devolverMovil(id);
					Integer idTipoMovil = Integer.parseInt(request.getParameter("idTipoMovil"));
					String descripcion = request.getParameter("descripcion");
					String patente = request.getParameter("patente");
					String gps = request.getParameter("gps");
					m.setTipoMovil(tma.devolverTipoMovil(idTipoMovil));
					m.setDescMovil(descripcion);
					m.setPatente(patente);
					m.setGps(gps);
					
					ma.editarMovil(m);
					jo.put("id", m.getIdMovil());
					jo.put("idTipoMovil", m.getTipoMovil().getIdTipoMovil());
					jo.put("descripcion", m.getDescMovil());
					jo.put("patente", m.getPatente());
					jo.put("gps", m.getGps());
					jo.put("error", false);
				} 
				catch (Exception e1) {
					try {
						jo.put("error", true);
					} catch (JSONException e) {
						e.printStackTrace();
					}
					e1.printStackTrace();
				}
			}
		}else if("nuevo".equals(request.getParameter("type"))){
			if(true){
				Integer idTipoMovil = Integer.parseInt(request.getParameter("idTipoMovil"));
				String descripcion = request.getParameter("descripcion");
				String patente = request.getParameter("patente");
				String gps = request.getParameter("gps");
				
				try {
					m = new Movil(null, descripcion, tma.devolverTipoMovil(idTipoMovil), gps, patente, true);
					
					try {
					   jo.put("action", "nuevo");
					   m = ma.crearMovil(m);
					   jo.put("id", m.getIdMovil());
					   jo.put("idTipoMovil", m.getTipoMovil().getIdTipoMovil());
					   jo.put("descripcion", m.getDescMovil());
					   jo.put("patente", m.getPatente());
					   jo.put("gps", m.getGps());
					   jo.put("error", false);
					} 
					catch (Exception e) {
						try {
							jo.put("error", true);
						} catch (JSONException e1) {
							e1.printStackTrace();
						}
						e.printStackTrace();
					}
				}catch (Exception e){
					e.printStackTrace();
				}
			}
		}
		response.setContentType("application/json");
		response.getWriter().write(jo.toString());
	}

}
